package com.maibo.lys.useddelete;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;

import java.util.HashSet;
import java.util.Random;
import java.util.Set;

/**
 * Created by LYS on 2017/3/1.
 */

public class CustomTitleView extends View {

    private String titleText;
    private int titleSize;
    private int titleColor;
    Paint mPaint;
    Rect bound;

    public CustomTitleView(Context context) {
        this(context, null);
    }

    public CustomTitleView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomTitleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        Log.e("重绘","构造方法");
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.CustomTitleView1, defStyleAttr, 0);
        int count = array.getIndexCount();
        for (int i = 0; i < count; i++) {
            int attr = array.getIndex(i);
            switch (attr) {
                case R.styleable.CustomTitleView1_TitleColor:
                    titleColor = array.getColor(attr, Color.BLACK);
                    break;
                case R.styleable.CustomTitleView1_TitleSize:
                    //将sp转换成pix
                    titleSize = array.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics()));
                    break;
                case R.styleable.CustomTitleView1_TitleText:
                    titleText = array.getString(attr);
                    break;
            }
        }
        array.recycle();
        mPaint = new Paint();
        mPaint.setTextSize(titleSize);
        bound = new Rect();
        //将文本放在矩形框中来测量文本的宽和高
//        mPaint.getTextBounds(titleText,0,titleText.length(),bound);
        this.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                titleText=randomText();
                //只走onDraw()方法
                postInvalidate();
            }
        });
    }


    private String randomText()
    {
        Random random = new Random();
        Set<Integer> set = new HashSet<>();
        while (set.size() < 4)
        {
            int randomInt = random.nextInt(10);
            set.add(randomInt);
        }
        StringBuffer sb = new StringBuffer();
        for (Integer i : set)
        {
            sb.append("" + i);
        }

        return sb.toString();
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        Log.e("重绘","测量");

        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int width;
        int height;
        if (widthMode == MeasureSpec.EXACTLY) {
            width = widthSize;
        } else {
            mPaint.setTextSize(titleSize);
            mPaint.getTextBounds(titleText, 0, titleText.length(), bound);
            float textWidth = bound.width();
            int desired = (int) (getPaddingLeft() + textWidth + getPaddingRight());
            width = desired;
        }

        if (heightMode == MeasureSpec.EXACTLY) {
            height = heightSize;
        } else {
            mPaint.setTextSize(titleSize);
            mPaint.getTextBounds(titleText, 0, titleText.length(), bound);
            float textHeight = bound.height();
            int desired = (int) (getPaddingTop() + textHeight + getPaddingBottom());
            height = desired;
        }

        setMeasuredDimension(width, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Log.e("重绘","绘制");
        //Log.e("宽和高","getMeasuredWidth:"+getMeasuredWidth()+"  getMeasuredHeight:"+getMeasuredHeight()+"  getWidth:"+getWidth()+"  getHeight:"+getHeight());
        mPaint.setColor(Color.YELLOW);
        canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), mPaint);
        mPaint.setColor(titleColor);
        canvas.drawText(titleText, getWidth() / 2 - bound.width() / 2, getHeight() / 2 + bound.height() / 2, mPaint);
    }


}
